package com.herui.iot.ui.evaluation;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.herui.sdyu_lib.utils.StringUtils;
import com.herui.sdyu_lib.utils.ToastUtil;
import com.herui.sport_care.R;
import com.herui.sport_care.data.eval.AgreementInfo;
import com.herui.sport_care.data.eval.BetaSchemeInfo;
import com.herui.sport_care.data.eval.BetaSchemeType;
import com.herui.sport_care.data.eval.OrganTreeInfo;
import com.herui.sport_care.data.eval.TestUserInfo;
import com.herui.sport_care.ui.BaseSportActivity;
import com.herui.sport_care.ui.dialog.DateChoseDialog;
import com.herui.sport_care.ui.dialog.PopDialog;
import com.herui.sport_care.ui.dialog.UserChoseDialog;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class EvalAddActivity extends BaseSportActivity {

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    @BindView(R.id.edit_name)
    EditText edit_name;
    @BindView(R.id.view_betaScheme)
    View view_betaScheme;
    @BindView(R.id.tv_betaScheme)
    TextView tv_betaScheme;
    @BindView(R.id.view_repeat_warn)
    View view_repeat_warn;

    @BindView(R.id.view_agreement)
    View view_agreement;
    @BindView(R.id.tv_agreement)
    TextView tv_agreement;
    @BindView(R.id.tv_power)
    TextView tv_power;

    @BindView(R.id.view_chose_date)
    View view_chose_date;
    @BindView(R.id.tv_date)
    TextView tv_date;

    private EvalPresenter evalPresenter;

    private AgreementInfo agreementInfo;           // 评估方案所用测试协议id
    private BetaSchemeInfo betaSchemeInfo;          // 评估方案所用测试方案id
    private Long choseTime;

    private List<TestUserInfo> allTestUserInfos;
    private List<TestUserInfo> selectTestUserInfos;
    private OrganTreeInfo organTreeInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("新增评估方案");

        evalPresenter = new EvalPresenter(view);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_eval_add;
    }

    @Override
    public void initParam() {

    }

    @OnClick({R.id.view_betaScheme, R.id.view_agreement, R.id.view_chose_date, R.id.btn_see_user,
            R.id.btn_save, R.id.btn_cancell})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.view_betaScheme:
                if (selectTestUserInfos == null || selectTestUserInfos.size() == 0) {
                    ToastUtil.showMessage("请先选择用户");
                    return;
                }
                evalPresenter.getBetaScheme();
                break;
            case R.id.view_agreement:
                if (betaSchemeInfo == null) {
                    ToastUtil.showMessage("请先选择测试方案");
                    return;
                }
                BetaSchemeType type = betaSchemeInfo.getType();
                if ((type.equals(BetaSchemeType.POWER_CAR_LADDER) || type.equals(BetaSchemeType.POWER_CAR_SLOPE))
                        && selectTestUserInfos.size() > 1) {
                    ToastUtil.showMessage("功率车不能选择多人");
                    return;
                }

                evalPresenter.getAgreement(betaSchemeInfo.getId());
                break;
            case R.id.view_chose_date:
                new DateChoseDialog(this, new DateChoseDialog.DialogLis() {
                    @Override
                    public void onOk(long date) {
                        choseTime = date;
                        tv_date.setText(format.format(new Date(choseTime)));
                    }
                });
                break;
            case R.id.btn_see_user:
                new UserChoseDialog(this, new UserChoseDialog.DialogLis() {
                    @Override
                    public void onOk( List<TestUserInfo> all, List<TestUserInfo> select, OrganTreeInfo organTreeInfo) {
                        allTestUserInfos = all;
                        EvalAddActivity.this.selectTestUserInfos = select;
                        EvalAddActivity.this.organTreeInfo = organTreeInfo;
                    }
                }, allTestUserInfos, organTreeInfo);
                break;
            case R.id.btn_save:
                String name = edit_name.getText().toString();
                if (StringUtils.isEmpty(name)){
                    ToastUtil.showMessage("方案名称不能为空");
                    return;
                }
                if (name.length() > 12){
                    ToastUtil.showMessage("方案名称过长");
                    return;
                }

                if (selectTestUserInfos == null || selectTestUserInfos.size() == 0){
                    ToastUtil.showMessage("请选择测试用户");
                    return;
                }

                if (betaSchemeInfo == null ){
                    ToastUtil.showMessage("请选择评估方案");
                    return;
                }
                if (agreementInfo == null ){
                    ToastUtil.showMessage("请选择协议");
                    return;
                }
                if (choseTime == null ){
                    ToastUtil.showMessage("请选择时间");
                    return;
                }

                StringBuilder builder = new StringBuilder();
                TestUserInfo testUserInfo = selectTestUserInfos.get(0);
                builder.append(testUserInfo.getId().toString());
                for (int i = 1; i < selectTestUserInfos.size(); i++) {
                    builder.append(",");
                    builder.append(selectTestUserInfos.get(i).getId().toString());
                }
                evalPresenter.create(name, format.format(new Date(choseTime)),
                        betaSchemeInfo.getId(), agreementInfo.getId(), builder.toString());

                break;
            case R.id.btn_cancell:
                onBackPressed();
                break;
            default:
                break;
        }

    }

    EvalPresenter.View view = new EvalPresenter.View() {

        @Override
        public void onCreateSucess() {
            onBackPressed();
        }

        @Override
        public void onNameError() {
            view_repeat_warn.setVisibility(View.VISIBLE);
        }

        @Override
        public void onBetaSchemeInfo(List<BetaSchemeInfo> betaSchemeInfos) {
            List<String> collect = new LinkedList<>();
            for (BetaSchemeInfo betaSchemeInfo : betaSchemeInfos) {
                collect.add(betaSchemeInfo.getName());
            }

            new PopDialog(EvalAddActivity.this, view_betaScheme, R.layout.item_pop_eval_value,
                    collect, "", new PopDialog.SelectLis() {
                @Override
                public void onSelect(String value) {
                    for (BetaSchemeInfo betaSchemeInfo : betaSchemeInfos) {
                        if (betaSchemeInfo.getName().equals(value)) {
                            EvalAddActivity.this.betaSchemeInfo = betaSchemeInfo;
                            tv_betaScheme.setText(betaSchemeInfo.getName());

                            BetaSchemeType type = betaSchemeInfo.getType();
                            if ((type.equals(BetaSchemeType.POWER_CAR_LADDER) || type.equals(BetaSchemeType.POWER_CAR_SLOPE))) {
                                if (selectTestUserInfos.size() > 1){
                                    tv_power.setText("预计功率--W");
                                    ToastUtil.showMessage("功率车不能选择多人");
                                }else{
                                    evalPresenter.calculatePower(selectTestUserInfos.get(0).getId().intValue());
                                }
                            }else{
                                tv_power.setText("预计功率--W");
                            }
                            break;
                        }
                    }
                }
            });
        }

        @Override
        public void onAgreementInfo(List<AgreementInfo> agreementInfos) {
            List<String> collect = new LinkedList<>();
            for (AgreementInfo agreementInfo : agreementInfos) {
                collect.add(agreementInfo.getName());
            }

            new PopDialog(EvalAddActivity.this, view_agreement, R.layout.item_pop_eval_value,
                    collect, "", new PopDialog.SelectLis() {
                @Override
                public void onSelect(String value) {
                    for (AgreementInfo agreementInfo : agreementInfos) {
                        if (agreementInfo.getName().equals(value)) {
                            EvalAddActivity.this.agreementInfo = agreementInfo;
                            tv_agreement.setText(agreementInfo.getName());

                            break;
                        }
                    }
                }
            });
        }

        @Override
        public void onPower(Double value) {
            tv_power.setText(String.format("预计功率%.2fW", value));
        }
    };
}